<template>
  <el-card
    class="box-card"
    :style="{ left: x + 'px', top: y + 'px' }"
    :stop="11"
    v-show="isConfig"
    @click.stop=""
    style="padding: 0"
  >
    <div
      class="text item"
      v-if="item.type === 'edge'"
      @click.stop="callBack('remove')"
    >
      删除
    </div>
    <div class="text item" v-if="isNode" @click.stop="lastDep()">
      上游依赖
    </div>
    <div class="text item" v-if="isNode" @click.stop="nextDep()">
      下游依赖
    </div>
    <div
      class="text item"
      v-if="item.type === 'example-node'"
      @click.stop="modifyStatus('3')"
    >
      暂停
    </div>
    <div
      class="text item"
      v-if="item.type === 'example-node'"
      @click.stop="modifyStatus('6')"
    >
      置虚
    </div>
    <div
      class="text item"
      v-if="item.type === 'example-node'"
      @click.stop="modifyStatus('1')"
    >
      重跑
    </div>
  </el-card>
</template>

<script>
import taskDefApi from "@/api/cycleJob/taskDef";
import task from '@/api/cycleJob/task'

export default {
  name: "MenuBar",
  props:{
    bizDate:String,
    nodeType:String
  },
  data() {
    return {
      x: "",
      y: "",
      item: {},
      isConfig: false,
      nodeCode: ''
    };
  },
  mounted() {},
  computed:{
    isNode(){
      return this.item.type?.split('-')[1]
    },
  },
  methods: {
    initFn(x, y, item) {
      this.x = parseInt(x) + "";
      this.y = y + "";
      this.item = item;
      if (this.isNode === "node") {
        if (this.item.item.store.data.data.children === undefined) {
          this.isConfig = true;
        }
        this.nodeCode = this.item.item.getData().label
      } else {
        this.isConfig = true;
      }
    },
    callBack(type) {
      this.$emit("callBack", type, this.item);
    },
    async lastDep() {
      this.callBack("operation");
      this.$parent.$data.loading = true
      if(this.nodeType === 'taskDef'){
        //console.log('走任务定义')
        await taskDefApi.getLastDepInfo(this.nodeCode).then((res) => {
          this.$parent.$data.loading = false
          if(res.data){
            if(res.data.lastDep?.length !== 0){
              this.lastDeps = res.data.lastDep;
            }
          }else {
            this.$notify({
              title: '该任务不存在上游依赖',
              type: 'warning',
              duration: 1500
            })
          }
        })
            .catch((err) => {
              this.$parent.$data.loading = false
              console.log(err);
            });
      }else {
        //console.log('走任务实例')
        await taskDefApi.getLastDepListInfo(this.nodeCode, this.bizDate).then((res) => {
          this.$parent.$data.loading = false
          if(res.data){
            if(res.data.lastDep?.length !== 0){
              this.lastDeps = res.data.lastDep;
            }else {
              this.$notify({
                title: '该任务上游依赖未生成实例',
                type: 'warning',
                duration: 1500
              })
            }
          }else {
            this.$notify({
              title: '该任务不存在上游依赖',
              type: 'warning',
              duration: 1500
            })
          }
        })
            .catch((err) => {
              this.$parent.$data.loading = false
              console.log(err);
            });
      }
      this.$parent.show(this.lastDeps, this.item.item, 1);
      //this.$parent.showNodeStatus(Object.assign([], this.statusList[1]));
    },
    async nextDep() {
      this.callBack("operation");
      this.$parent.$data.loading = true
      if(this.nodeType === 'taskDef'){
        await taskDefApi.getNextDepInfo(this.nodeCode).then((res) => {
          this.$parent.$data.loading = false
          if(res.data){
            if(res.data.nextDep?.length !== 0){
              this.nextDeps = res.data.nextDep;
            }
          }else {
            this.$notify({
              title: '该任务不存在下游依赖',
              type: 'warning',
              duration: 1500
            })
          }
        })
            .catch((err) => {
              this.$parent.$data.loading = false
              console.log(err);
            });
      }else {
        console.log('走任务实例', this.bizDate)
        await taskDefApi.getNextDepListInfo(this.nodeCode, this.bizDate).then((res) => {
          this.$parent.$data.loading = false
          if(res.data){
            if(res.data.nextDep?.length !== 0){
              this.nextDeps = res.data.nextDep;
            }else {
              this.$notify({
                title: '该任务下游依赖未生成实例',
                type: 'warning',
                duration: 1500
              })
            }
          }else {
            this.$notify({
              title: '该任务不存在下游依赖',
              type: 'warning',
              duration: 1500
            })
          }
        })
            .catch((err) => {
              this.$parent.$data.loading = false
              console.log(err);
            });
      }
      this.$parent.show(this.nextDeps, this.item.item, -1);
      //this.$parent.showNodeStatus(Object.assign([], this.statusList[0]));
    },
    modifyStatus(newStatus) {
      this.callBack("operation");
      let formData = {
        periodTaskList: [this.item.item.store.data.periodTask],
        taskStatus: newStatus
      };
      task.batchChangeStatus(formData)
          .then((res) => {
            let msg =
                "更新【" +
                res.upCount +
                "】个任务,成功【" +
                res.successCount +
                "】个！";
            this.$notify({
              title: msg,
              type: "success",
            });
            if(res.successCount){
              taskDefApi.initTaskInfo(this.nodeCode, this.bizDate)
                .then((res) => {
                  this.item.item.store.data.periodTask = res.periodTask
                })
                .catch((err) => {
                  console.log(err);
                });
              let status
              let statusDesc
              const data = this.item.item.getData();
              if(newStatus === '3'){
                status = 'default'
                statusDesc = '任务暂停'
              }else if(newStatus === '6'){
                status = 'success'
                statusDesc = '置虚'
              }else {
                status = 'default'
                statusDesc = '待处理'
              }
              this.item.item.setData({
                ...data,
                status,
                statusDesc
              });
            }
          })
          .catch(() => {
            this.$notify({
              title: "修改状态失败",
              type: "error"
            })
          });
    }
  },
};
</script>
<style scoped>
.box-card {
  position: absolute;
  z-index: 112;
  width: 150px;
}

.text {
  font-size: 14px;
}

.item {
  padding: 10px 0;
  text-align: center;
}

.item:hover {
  color: #ffffff;
  background-color: #409eff;
}
</style>
